#include <cstdlib>
#include <ctime>
#include <set>


inline
mat
aux_functions::convert2Arma(cv::Mat cvImage)
{
  
  running_stat<double> stats;
  
  mat armaImage(cvImage.rows, cvImage.cols);
  
  for (uword row = 0; row < cvImage.rows; row++)
  {
    for (uword col = 0; col< cvImage.cols; col++)
    {
      armaImage(row,col) =  cvImage.at<uchar> (row, col);
      stats(armaImage(row,col));
    }
  }
  
  return armaImage;
}


inline
mat
aux_functions::convert2Arma_nor(cv::Mat cvImage)
{
  
  running_stat<double> stats;
  
  mat armaImage(cvImage.rows, cvImage.cols);
  
  for (uword row = 0; row< cvImage.rows; row++)
  {
    for (uword col = 0; col< cvImage.cols; col++)
    {
      armaImage(row,col) =  cvImage.at<uchar> (row, col);
      stats(armaImage(row,col));
    }
  }
  //cout<< "Normalization:" << endl;
  armaImage = (armaImage - stats.mean())/stats.stddev();
  
  
  
  /*
   * checking if mean = 0 && std =1
 running_stat<double> stats2;
   for (uword row = 0; row< cvImage.rows; row++)
  {
    for (uword col = 0; col< cvImage.cols; col++)
    {
      
      stats2(armaImage(row,col));
    }
  }
  
  cout << "mean: " << stats2.mean() << ". std= " << stats2.stddev() << endl;
  getchar();
  
  */
  return armaImage;
}





inline
mat
aux_functions::convert2Arma(cv::Mat cvImage, double rescal)
{
  resize(cvImage, cvImage, cv::Size( cvImage.cols/rescal,cvImage.rows/rescal) );
  //cout << "Actual size = ( " << armaImage.n_rows << " , " << armaImage.n_cols  << " )"<< endl;
  running_stat<double> stats;
  
  mat armaImage(cvImage.rows, cvImage.cols);
  
  for (uword row = 0; row< cvImage.rows; row++)
  {
    for (uword col = 0; col< cvImage.cols; col++)
    {
      armaImage(row,col) =  cvImage.at<uchar> (row, col);
      stats(armaImage(row,col));
    }
  }
  //cout<< "Normalization:" << endl;
  //armaImage = (armaImage - stats.mean())/stats.stddev();
  //cout << "Actual size = ( " << armaImage.n_rows << " , " << armaImage.n_cols  << " )"<< endl;
  
  /*if ( armaImage.n_rows != floor(ima_rows/rescal) || armaImage.n_cols != floor(ima_cols/rescal))  
   *  { *
   *  cout << "Check image size"  << endl;
   *  cout << "Actual size = ( " << armaImage.n_rows << " , " << armaImage.n_cols  << " )"<< endl;
}*/
  
  return armaImage;
}

inline
uvec
aux_functions::rand_vector(int n, int min, int max) // Random int vector between  [min, max]. Vectr size = n
{
  uvec vector(n);
  int i = 0;
  srand(time(NULL));

  std::set<int> unique_set;
  while(unique_set.size() < n)
  {
    unique_set.insert((rand() % (max - min)) + min);
  }
  for(std::set<int>::iterator it = unique_set.begin(); it != unique_set.end(); it++)
  {
    //std::cout << *it << "  ";
    vector(i) = *it;
    i++;
  }
  //vector.t().print("vector");
  return vector;
}
